Latest update: June 2013
In this tutorial, we will show you how to download content from your FlashAir device. We will take the information from the content list that we created using command.cgi and make an application that downloads and displays a selected image file.
This tutorial follows
iOS Tutorial 2: Getting A List of Contents in
the FlashAir iOS app tutorial series.
Let's try making this app.
Here is the screen layout that we would like to create:
Once the application is launched, the list should display the contents of the current folder in the TableView block, the number of files in the labelCount block and the name of the directory to the labelDirectory block.
We will add the following elements:
UIButton
)
UILabel
)
UITableView
)
UITableViewCell
)
UIViewController
) UIImageView
)
UINavigationController
)
We will create a content list that looks like this:
If a folder name in the list is tapped, we will show the contents of that folder.
If "back" is tapped, we will move one element up the path and display the contents
of that folder.
If an image file in the list is tapped, we will display the image like this:
Image files are displayed using a ViewController. The ViewController is set in the class:
An image file can be acquired without using CGI commands if you specify the file path directly (in a browser or file explorer). To get data from the image file, we will use the variable NSData dataWithContentsOfURL . This function will return the data object.
@interface FSImageViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic) NSString *fileInfo;
@end
fileInfo
: is a property of View Controller that stores the content list
file information
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Make a file path
NSString *dir =
[[self.fileInfo componentsSeparatedByString:@","] objectAtIndex:0];
NSString *filename =
[[self.fileInfo componentsSeparatedByString:@","] objectAtIndex:1];
NSString *filePath =
[[dir stringByAppendingString:@"/"] stringByAppendingString:filename];
// Run
NSURL *url =
[NSURL URLWithString:[NSString stringWithFormat:@"http://flashair/%@", filePath]];
// Get image data
if(nil == url)return;
NSData *img_data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:img_data];
// Display results
self.imageView.image = img;
}
To get the content list, we use
command.cgi
with
op=100
and
op=101
.
For information on how to get the contents list, please see
iOS Tutorial 2: Getting a Content List.
We will set the results of the cgi command to a Table View.
#import <UIKit/UIKit.h>
#import "FSImageViewController.h"
@interface FSViewController : UIViewController{
@private
NSArray *files;
NSString *count;
NSString *rowdata;
}
@property (strong, nonatomic) IBOutlet UILabel *labelCount;
@property (strong, nonatomic) IBOutlet UILabel *labelDirectory;
@property (strong, nonatomic) IBOutlet UITableView *tableViewFileList;
- (IBAction)buttonPush:(id)sender;
- (void)getFileList:(NSString*)path;
@end
FSImageViewController.h
so that when you click an item in the list, the
rowdata
property of
FSViewController
will be sent to the
ImageViewController
that fetches and displays the image. (The value of
rowdata
is stored in
fileInfo
.)files
,
count
: store the results from
command.cgi
with
op=100
and
op=101
respectively.
rowdata
: manages the file information for the selected rows- (void)getFileList:(NSString *)path{
NSError *error = nil;
// Get file list
// Make url
NSURL *url100 = [NSURL URLWithString:[@"http://flashair/command.cgi?op=100&DIR="
stringByAppendingString: path]];
// Run cgi
NSString *dirStr = [NSString stringWithContentsOfURL:url100
encoding:NSUTF8StringEncoding error:&error];
if ([error.domain isEqualToString:NSCocoaErrorDomain]){
NSLog(@"error100 %@\n",error);
return;
}
files = [dirStr componentsSeparatedByString:@"\n"];
// Get the number of files
// Make url
NSURL *url101 = [NSURL URLWithString:[@"http://flashair/command.cgi?op=101&DIR="
stringByAppendingString: path]];
// Run cgi
NSString *cntStr = [NSString stringWithContentsOfURL:url101
encoding:NSUTF8StringEncoding error:&error];
if ([error.domain isEqualToString:NSCocoaErrorDomain]) {
NSLog(@"error101 %@\n",error);
return;
}
count = cntStr;
// Display results
self.labelCount.text = [@"Items Found:" stringByAppendingString:cntStr];
if(![path isEqualToString:@"/"]){
self.labelDirectory.text = [path stringByAppendingString:@"/" ];
}else{
self.labelDirectory.text = @"/";
}
}
command.cgi
with
op=100
and set the results to the appropriate variables.command.cgi
with
op=101
and set the results to the appropriate variables. labelDirectory
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [count intValue];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
NSString *filename = [[[files objectAtIndex:indexPath.row + 1]
componentsSeparatedByString:@","] objectAtIndex:1];
unsigned char attribute = [[[[files objectAtIndex:indexPath.row + 1]
componentsSeparatedByString:@","] objectAtIndex:3] intValue];
// If it is folder
if ((attribute & 0x10) != 0) {
filename = [filename stringByAppendingString:@"/" ];
}else{
NSArray *name_array = [filename componentsSeparatedByString:@"."];
NSString *ext = [[name_array objectAtIndex:[name_array count]-1] lowercaseString];
if (!([ext isEqualToString:@"jpg"] || [ext isEqualToString:@"jpeg"] ||
[ext isEqualToString:@"png"] || [ext isEqualToString:@"jpe"])) {
[cell setUserInteractionEnabled:NO];
}
}
cell.textLabel.text = filename;
return cell;
}
textLabel
: stores the name of the retrieved file. We will set up the touch behavior of the content list. We will set behaviors for when the user taps the back button or an image file or directory in the content list.
These are the operations we are running when the list is tapped:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
rowdata = [files objectAtIndex:indexPath.row + 1];
NSString *dir = [[rowdata componentsSeparatedByString:@","] objectAtIndex:0];
NSString *filename = [[rowdata componentsSeparatedByString:@","] objectAtIndex:1];
NSString *filePath = [[dir stringByAppendingString:@"/"] stringByAppendingString:filename];
// If it is folder
if(([[[rowdata componentsSeparatedByString:@","] objectAtIndex:3] intValue] & 0x10) != 0){
[self getFileList:filePath];
[self.tableViewFileList reloadData];
}else{
[self performSegueWithIdentifier:@"imageView" sender:self];
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Give next View the Data
if ([segue.identifier isEqualToString:@"imageView"]) {
FSImageViewController *iamgeViewController = segue.destinationViewController;
iamgeViewController.fileInfo = rowdata;
}
}
command.cgi
to get the contents of the directory that was selected. We set
this to
a Table View as well. Above, we have set the behavior of the back button.
- (IBAction)buttonPush:(id)sender {
NSString *path = [self.labelDirectory.text
substringToIndex:[self.labelDirectory.text length] - 1];
NSRange found = [path rangeOfString:@"/" options:NSBackwardsSearch];
if(found.location != NSNotFound){
if (found.location == 0) {
path = @"/";
}else{
path = [path substringToIndex:found.location];
}
}else{
path = @"/";
}
// Reload tableview
[self getFileList:path];
[self.tableViewFileList reloadData];
}
command.cgi
again on this path to show the contents list. We set this to a
Table View.Once we have completed writing the program, we will check to see if it works. If we tap "IMG_2340.JPG" in the content list shown above (in the example Table View screenshot earlier in this tutorial), we should see that image file displayed like this:
You have now completed the tutorial on downloading content.
ios_tutorial_03.zip (25KB)
All sample code on this page is licensed under BSD 2-Clause License